import json

from django.http import HttpResponse
from django.shortcuts import render,render_to_response
from JobData.models import Data51JobShe, DataLagou, DataXiaomi, DataXiaomiShe

# Create your views here.
def getallData(request):
    limit = 15
    startPage = 0
    endPage = 15
    page = 1
    if request.GET.get('limit') is not None:
        limit = int(request.GET.get('limit'))
    if request.GET.get('page') is not None:
        page = int(request.GET.get('page'))
        endPage = page * limit
        startPage = endPage - limit
    keyWord = request.GET.get('key')
    orgin = {"code": 0, "msg": "", "count": 0, "data": []}
    if keyWord == "51job":
        count = Data51JobShe.objects.all().count()
        if endPage > count:
            endPage = count
        dictData = list(Data51JobShe.objects.all()[startPage:endPage].values())
    elif keyWord == "xiaom":
        count = DataXiaomiShe.objects.all().count()
        if endPage > count:
            endPage = count
        dictData = list(DataXiaomiShe.objects.all()[startPage:endPage].values()) + list(DataXiaomi.objects.all()[:limit].values())
    else:
        count = DataLagou.objects.all().count()
        if endPage > count:
            endPage = count
        dictData = list(DataLagou.objects.all()[startPage:endPage].values())
    orgin['data'] = dictData
    orgin['count'] = count
    print(dictData)
    print(count)
    return HttpResponse(json.dumps(orgin))

def showPage(request):
    keyWord = request.GET.get('key')
    if keyWord == "51job":
        return render_to_response('admin/dataManage51job.html')
    elif keyWord == "xiaom":
        return render_to_response('admin/dataManagexiaomi.html')
    else:
        return render_to_response('admin/dataManagelagou.html')